package com.zjw.video.custom.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjw.video.common.entity.TagEntity;
import com.zjw.video.common.entity.req.TagReqVO;
import com.zjw.video.common.entity.resp.TagRespVO;
import com.zjw.video.common.mapper.TagMapper;
import com.zjw.video.custom.service.TagService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, TagEntity> implements TagService {

    @Override
    public Page<TagRespVO> tagList(TagReqVO tagReqVO) {
        Page<TagEntity> tagEntityPage = lambdaQuery()
                .like(TagEntity::getName, tagReqVO.getName())
                .page(new Page<>(tagReqVO.getPageNum(), tagReqVO.getPageSize()));
        Page<TagRespVO> tagRespVOPage = new Page<>();
        tagRespVOPage.setCurrent(tagEntityPage.getCurrent());
        tagRespVOPage.setPages(tagEntityPage.getPages());
        tagRespVOPage.setSize(tagEntityPage.getSize());
        tagRespVOPage.setTotal(tagEntityPage.getTotal());
        List<TagRespVO> tagRespVOS = new ArrayList<>();
        List<TagEntity> records = tagEntityPage.getRecords();
        for (TagEntity record : records) {
            TagRespVO tagRespVO = new TagRespVO();
            BeanUtils.copyProperties(record, tagRespVO);
            tagRespVOS.add(tagRespVO);
        }
        tagRespVOPage.setRecords(tagRespVOS);
        return tagRespVOPage;
    }
}
